/**
 * axios 请求
 *
 * @author liuqi
 * @date 2024/12/21 17:34
 **/
import axios from "axios";
import setter from "../setting/setter";
import tools from "./Tools";
import Vue from "vue";

class AxiosRequest {
    static init = function () {
        tools.merge(axios.defaults, setter.server.request);
    }

    /**
     * 请求配置项
     */
    config;

    /**
     * 构造函数
     *
     * @param AxiosRequestConfig
     */
    constructor(AxiosRequestConfig) {
        this.config = AxiosRequestConfig;
    }

    /**
     * 发起请求
     *
     * @returns {CustomizePromise}
     */
    send() {
        return axios.request(this.config).then(response => {
            // 状态
            let state = response.data[setter.server.response.format.state];
            // 如果响应状态码不等于 0
            if (state != setter.server.response.state.ok) {
                // 抛出错误
                return Promise.reject(response.data);
            } else {
                return response.data;
            }
        }).catch(error => {
            let title = error.msg ? "操作失败" : "系统服务异常";
            let message = error.msg || error.message;
            Vue.prototype.$notify.error({
                title: title,
                message: message
            });
            return Promise.reject(error);
            //return new Promise(() => {});
        });
    }
}
export default AxiosRequest;
